package com.app.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.app.common.Result;
import com.app.model.OptionModel;
import com.app.model.QuestionAnswerModel;
import com.app.model.QuestionModel;
import com.app.service.QuestionnaireService;
import com.app.vo.QuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 问卷调查
 */
@Controller
@RestController
@RequestMapping("/api/questionnaire")
public class QuestionnaireController {
    @Autowired
    private QuestionnaireService questionnaireService;

    //查询调查问卷问题
    @GetMapping("/all")
    public Result<Object> getAll() {
        QuestionVo all = questionnaireService.getAll();
        return Result.success(all);
    }

    //保存答案  /api/questionnaire/save   请求方式post  参数 json对象，userId
    @PostMapping("/save")
    public Result<Object> saveOption(@RequestBody JSONObject json) {
        JSONArray jsonArray = JSONArray.parseArray(JSONObject.toJSONString(json.get("json")));
        Integer userId = (Integer) json.get("userId");
        questionnaireService.saveOption(jsonArray, userId);
        return Result.success("提交成功！");
    }

    //问卷调查设计
    //添加 名称 父级 是否多选 状态 排序   如果没有父级 就是问题
    @PostMapping("/insertAndUpdate")
    public Result<Object> insertQuestionAndOption(@RequestBody OptionModel model) {
        if (model.getId() == 0) {
            questionnaireService.insertQuestionAndOption(model);
        } else {
            questionnaireService.updateQuestionAndOption(model);
        }
        return Result.success("操作成功！");
    }

    //删除
    @GetMapping("/deleteQuestionAndOption")
    public Result<Object> deleteQuestionAndOption(Integer id) {
        questionnaireService.deleteQuestionAndOption(id);
        return Result.success("更新成功！");
    }


    //查询父级列表
    @GetMapping("/selectParentList")
    public Result<Object> selectParentList() {
        QuestionVo all = questionnaireService.getAll();
        return Result.success(all);
    }

    //问卷列表查询
    @GetMapping("/selectQuestion")
    public Result<Object> selectQuestion(String name) {
        List<QuestionModel> all = questionnaireService.selectQuestion(name);
        return Result.success(all);
    }

    //状态更新
    @GetMapping("/updateStatus")
    public Result<Object> updateStatus(Integer status,Integer id) {
       questionnaireService.updateStatus(status,id);
        return Result.success("操作成功！");
    }

    //查询参与问卷用户
    @GetMapping("/selectAnswer")
    public Result<Object> selectAnswer(Integer page, Integer limit, String keyword) {
        return Result.success(questionnaireService.selectAnswer(page, limit, keyword));
    }

    //查询用户问卷详情
    @GetMapping("/selectAnswerContent")
    public Result<Object> selectAnswerContent(Integer userId, Integer page, Integer limit) {
        return Result.success(questionnaireService.selectAnswerContent(userId, page, limit));
    }
}
